package com.yx.yinshanjinniu.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.yx.yinshanjinniu.R;


public class LoadingDialog extends Dialog {

    private RotateAnimation mRotateAnimation;
    private ImageView iv_loading;

    public LoadingDialog(@NonNull Context context) {
        super(context, R.style.dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        iv_loading = findViewById(R.id.img);
        initView();
    }

    private void initView() {
        initAnimation();
        final Window window = getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        iv_loading.startAnimation(mRotateAnimation);
    }

    private void initAnimation() {
        if (null == mRotateAnimation) {
            mRotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            mRotateAnimation.setDuration(1000);
            mRotateAnimation.setRepeatCount(-1);
            mRotateAnimation.setFillAfter(false);
            mRotateAnimation.setInterpolator(new LinearInterpolator());
        }
    }

    @Override
    public void show() {
        super.show();
        mRotateAnimation.cancel();
        iv_loading.startAnimation(mRotateAnimation);
    }

}
